/****************************************************************************
 *
 * SPI Master library for Arduino STM32 + HAL + CubeMX (HALMX).
 *
 * Copyright (c) 2016 by Vassilis Serasidis <info@serasidis.gr>
 * Home: http://www.serasidis.gr
 * email: avrsite@yahoo.gr
 * 
 * Arduino_STM32 forum: http://www.stm32duino.com
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of either the GNU General Public License version 2
 * or the GNU Lesser General Public License version 2.1, both as
 * published by the Free Software Foundation.
 *
 ****************************************************************************/


#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED

#include <chip.h>
#include "variant.h"


#define SPI_CLOCK_DIV4 0x00
#define SPI_CLOCK_DIV16 0x01
#define SPI_CLOCK_DIV64 0x02
#define SPI_CLOCK_DIV128 0x03
#define SPI_CLOCK_DIV2 0x04
#define SPI_CLOCK_DIV8 0x05
#define SPI_CLOCK_DIV32 0x06 
#define SPI_CLOCK_DIV256 0x07

#define SPI_MODE0 SPI_MODE_SLAVE
#define SPI_MODE1 SPI_MODE_MASTER

#ifdef USE_SPI1
extern SPI_HandleTypeDef hspi1;
#endif
#ifdef USE_SPI2
extern SPI_HandleTypeDef hspi2;
#endif


class SPIClass {
  public:
    SPIClass(SPI_TypeDef *spiPort);
    SPIClass(uint8_t spiPort);
    void begin(void);
    
    uint8_t transfer(uint8_t _data) const;
    uint16_t transfer16(uint16_t data);
    void transfer(uint8_t buf, size_t count);
    
    void setBitOrder(uint8_t bitOrder);
    void setClockDivider(uint8_t clockDiv);
    void setDataMode(uint8_t dataMode);
    
  private:
    uint32_t _Mode;
    uint32_t _Direction;          
    uint32_t _DataSize;
    uint32_t _CLKPolarity;
    uint32_t _CLKPhase;
    uint32_t _NSS;
    uint32_t _BaudRatePrescaler;
    uint32_t _FirstBit;
    uint32_t _TIMode;
    uint32_t _CRCCalculation;
    uint32_t _CRCPolynomial; 
    
    SPI_HandleTypeDef *_hspi;
    SPI_TypeDef *_spiPort;    
    void init(void);

}; 

extern SPIClass SPI;

#endif